package org.budo.jdbc.driver.resultset.impl;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.budo.jdbc.driver.AbstractBudoJdbcResultSet;
import org.budo.jdbc.driver.BudoJdbcResultSetMetaData;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 单列多行结果集,纵向一维数组
 * 
 * @author lmw
 */
@Getter
@Setter
@NoArgsConstructor
public class ObjectArrayResultSet<T> extends AbstractBudoJdbcResultSet {
    private T[] valueArray;

    private int rowNumber = -1;

    public ObjectArrayResultSet(ResultSetMetaData metaData, T[] valueArray) {
        this.setMetaData(metaData);
        this.setValueArray(valueArray);
    }

    public ObjectArrayResultSet(String[] columnNames, T[] valueArray) {
        this(new BudoJdbcResultSetMetaData(columnNames), valueArray);
    }

    @Override
    public boolean next() throws SQLException {
        if (null == this.getValueArray() //
                || this.getValueArray().length <= this.rowNumber + 1) {
            return false;
        }

        this.rowNumber++;
        return true;
    }

    @Override
    protected Object getColumnValueRaw(String columnLabel) {
        int rowNumber = this.getRowNumber();
        return this.getValueArray()[rowNumber];
    }
}
